package com.sinosoft.cloud.jsonmaker.ant;

import com.alibaba.fastjson.JSONObject;
import com.sinosoft.cloud.annotation.BusinessType;
import com.sinosoft.cloud.annotation.ChannelType;
import com.sinosoft.cloud.annotation.ReqJsonBean;
import com.sinosoft.cloud.annotation.ReqMsgBody;
import com.sinosoft.cloud.rest.TradeInfo;
import com.sinosoft.cloud.util.GetBizNo;
import com.sinosoft.lis.entity.PolFactorPojo;

/**
 * 描述:
 *
 * @author 张旭东
 * @date 2018/11/27
 */
@ReqMsgBody(channelType = ChannelType.ANT, businessType = BusinessType.POL, name = "ant-pol", remark = "蚂蚁首期承保")
public class PolJsonBeanMaker extends ReqJsonBean {
    final static String signature = "QX01D/2hIHNQTF6xj9tVnZhn72SZ29azvxUcjeztZiXYQcQdGwR89dvT" +
            "aHltndv2j44GqEgOawUcyW2asbLH1eCVLdUW3L2R8QboBw66zEwpegvhG8Owvoc0fSSGlWG9EM" +
            "F7IfIe70zqMW8euWvgU8r9ij7zDKU+rGMUg+JWR0UMPZexO5Ue5DHjXtkkhsvr/0I" +
            "vgJFf0wRQx0E0XiCK4ZMwq+6EJ56SQjuaqRD/NBuxTtqKf5lBe2UhGb4YsmSPd" +
            "nyMcjvVKjByY+xBdv1FK+xw1q965HR8GYC1V4PZ0CkjJHViWjAAeHARfxFLBlnG" +
            "sFA8Z8B84Zqctefn1lNnUA==";

    @Override
    public JSONObject header(TradeInfo tradeInfo) {
        PolFactorPojo pojo = (PolFactorPojo) tradeInfo.getData(PolFactorPojo.class.getName());
        JSONObject header = new JSONObject();
        header.put("version", "1.0.0");
        header.put("function", "ant.bxcloud.info.issueNotify");
        header.put("transTime", GetBizNo.getFullDate(pojo.getApplyDate()));
        header.put("transTimeZone", "UTC+8");
        header.put("reqMsgId", GetBizNo.getReqMsgId());
        header.put("format", "json");
        header.put("signType", "RSA");
        header.put("asyn", "0");
        header.put("cid", "PICC");
        return header;
    }

    @Override
    public JSONObject body(TradeInfo tradeInfo) {
        PolFactorPojo pojo = (PolFactorPojo) tradeInfo.getData(PolFactorPojo.class.getName());
        //准备报文体
        JSONObject body = new JSONObject();
        body.put("outBizNo", GetBizNo.getOutBizNo());
        body.put("instSerialNo", GetBizNo.getInstSerialNo());
        body.put("policyNo", GetBizNo.getPolicyNo());
        body.put("prodNo", pojo.getProductCode());
        body.put("policyType", "2");
        body.put("premium", pojo.getPrem());
        body.put("actualPremium", pojo.getPrem());
        body.put("sumInsured", pojo.getAmnt());
        body.put("insuredTime", GetBizNo.getDate(pojo.getApplyDate()));
        body.put("issueTime", (pojo.getApplyDate()));
        body.put("effectStartTime", (pojo.getEffectiveDate()));
        body.put("effectEndTime", (pojo.getEffectiveDate()));
        body.put("applyNum", "2");

        // body下的bills
        JSONObject bills = new JSONObject();
        bills.put("billNo", "18031550015984040320");
        bills.put("merchantAccountType", "1");
        bills.put("merchantAccountId", "2088101164965391");
        bills.put("otherAccountType", "1");
        bills.put("otherAccountId", "2088302224968066");
        bills.put("payFlowId", "2018031510002001060204830643");
        bills.put("fee", "50000");
        bills.put("payTime", "2018412170903");

        // body下的 coverageList
        JSONObject coverageList = new JSONObject();
        String produce = pojo.getProduceCode2()==null?"":pojo.getProduceCode2();
        coverageList.put("coverageNo", produce);
        coverageList.put("csuNo", "6704_1");
        coverageList.put("premium", "5000");
        coverageList.put("sumInsured", "56415313");
        coverageList.put("effectStartTime", (pojo.getEffectiveDate()));
        coverageList.put("effectEndTime", "2018412170903");
        coverageList.put("copiesCount", "1");

        // body下的 holder
        JSONObject holder = new JSONObject();
        holder.put("personNo", "18031550020545320321");
        holder.put("certType", "100");
        holder.put("certNo", pojo.getAppntIdNo());
        holder.put("certName", pojo.getAppntName());
        holder.put("phone", "");
        holder.put("gender", "");
        holder.put("email", "");
        holder.put("birthday", "");
        holder.put("pronounceName", "");
        holder.put("nationality", "CH");

        // body下的 insureds
        JSONObject insureds = new JSONObject();
        insureds.put("personNo", "18031550020545420321");
        insureds.put("relationWithHolder", pojo.getRelation());
        insureds.put("certType", "100");
        insureds.put("certNo", pojo.getInsuredIdNo());
        insureds.put("certName", pojo.getInsuredName());
        insureds.put("phone", "");
        insureds.put("gender", "");
        insureds.put("email", "");
        insureds.put("birthday", "");
        insureds.put("pronounceName", "");
        insureds.put("nationality", "CH");
        String social = pojo.getSocialType()== null?"":pojo.getSocialType();
        insureds.put("medicalInsurance", social);
        insureds.put("waitingPeriod", "");

        body.put("bills", bills);
        body.put("coverageList", coverageList);
        body.put("holder", holder);
        body.put("insureds", insureds);
        return body;
    }

    @Override
    public String request(TradeInfo tradeInfo){
        JSONObject res = new JSONObject();
        JSONObject header = header(tradeInfo);
        JSONObject body = body(tradeInfo);
        JSONObject request = new JSONObject();
        request.put("header",header);
        request.put("body",body);
        res.put("signature",signature);
        res.put("request",request);
        return res.toJSONString();
    }
}
